package com.lzp.systemopt.camera.impl

import android.app.Activity
import android.content.Context
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.util.Log
import com.lzp.systemopt.camera.CaptureListener
import com.lzp.systemopt.camera.ICameraManager
import com.lzp.systemopt.camera.view.CameraSurfaceView
import com.utils.LogUtils

/**
 * @description:
 * @author: lxp
 * @date: 2022/1/22 上午10:24
 */
class Camera2Impl : ICameraManager {


    companion object {
        private const val TAG = "Camera2Impl"
    }

    private lateinit var mCameraManager: CameraManager


    private var pictureImpl: CapturePictureImpl? = null
    private var videoImpl: CaptureVideoImpl? = null

    private fun generatePictureImpl(): CapturePictureImpl {
        if (pictureImpl == null) {
            pictureImpl = CapturePictureImpl().apply {
                initCamera(activity, cameraSurfaceView)
            }
        }
        return pictureImpl!!
    }

    private fun generateVideoImpl(): CaptureVideoImpl {
        if (videoImpl == null) {
            videoImpl = CaptureVideoImpl().apply {
                initCamera(activity, cameraSurfaceView)
            }
        }
        return videoImpl!!
    }


    private lateinit var activity: Activity
    private lateinit var cameraSurfaceView: CameraSurfaceView

    override fun initCamera(activity: Activity, cameraSurfaceView: CameraSurfaceView) {
        this.activity = activity
        this.cameraSurfaceView = cameraSurfaceView
        mCameraManager = activity.getSystemService(Context.CAMERA_SERVICE) as CameraManager
    }

    override fun setCaptureListener(listener: CaptureListener?) {
        pictureImpl?.apply {
            setCaptureListener(listener)
        }
        videoImpl?.apply {
            setCaptureListener(listener)
        }
    }

    override fun requestSwitchCamera() {
        com.lzp.systemopt.camera.CameraManager.switchCameraId()
        pictureImpl?.requestSwitchCamera()
        videoImpl?.requestSwitchCamera()
    }

    override fun releaseCamera(reason: String) {
        pictureImpl?.releaseCamera(reason)
        videoImpl?.releaseCamera(reason)
        pictureImpl = null
        videoImpl = null
    }

    override fun startCapturePicPreView() {
        stopCaptureVideoPreview()
        generatePictureImpl().startCapturePicPreView()
    }

    override fun stopCapturePicPreView() {
        pictureImpl?.stopCapturePicPreView()
        pictureImpl = null
    }

    override fun capturePic() {
        pictureImpl?.capturePic()
    }

    override fun startCaptureVideoPreview() {
        stopCapturePicPreView()
        generateVideoImpl().startCaptureVideoPreview()
    }

    override fun stopCaptureVideoPreview() {
        videoImpl?.stopCaptureVideoPreview()
        videoImpl = null
    }

    override fun captureVideo() {
        videoImpl?.captureVideo()
    }

    override fun stopCaptureVideo() {
        videoImpl?.stopCaptureVideo()
    }
}